﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BUS;
using DAL;
namespace QuanLyNhaSach
{
    public partial class Form_Them_CapNhat_Sach : DevComponents.DotNetBar.Office2007Form
    {
        public delegate void ThemMoi();
        public delegate void CapNhat();

        public event ThemMoi KhiThemMoi = null;
        public event CapNhat KhiCapNhat = null;

        public string Action = "";
        public string ID = null;

        Sach_BUS sach_bus;
        TacGia_BUS tacgia_bus;
        NhaXuatBan_BUS nxb_bus;
        TheLoai_BUS theloai_bus;

        Sach_DTO sach_dto;
        public Form_Them_CapNhat_Sach()
        {
            InitializeComponent();
        }
        private void Form_Them_CapNhat_Sach_Load(object sender, EventArgs e)
        {
            //set man hinh luc loadform
            SetBounds(Screen.GetWorkingArea(this).Width / 4, Screen.GetWorkingArea(this).Height / 4, Width, Height);
            nxb_bus = new NhaXuatBan_BUS();
            cbxNXB.DataSource = nxb_bus.LayDanhSachNhaXuatBan();
            cbxNXB.DisplayMember = "Str_TenNXB";
            cbxNXB.ValueMember = "Str_MaNXB";

            tacgia_bus = new TacGia_BUS();
            cbxTacGia.DataSource = tacgia_bus.LayDanhSachTacGia();
            cbxTacGia.DisplayMember = "Str_TenTacGia";
            cbxTacGia.ValueMember = "Str_MaTacGia";

            theloai_bus = new TheLoai_BUS();
            cbxTheLoai.DataSource = theloai_bus.LayDanhSachTheLoai();
            cbxTheLoai.DisplayMember = "Str_TenTheLoai";
            cbxTheLoai.ValueMember = "Str_MaTheLoai";
            //them sach
            if (Action.Equals("Insert"))
            {
                sach_bus = new Sach_BUS();
                txtMaSach.Text = sach_bus.MaSachTang().ToString();
            }
            //cap nhat scah
            if (Action.Equals("Update"))
            {
                sach_bus = new Sach_BUS();
                sach_dto = new Sach_DTO();
                sach_dto = sach_bus.DanhSachSach_TheoMaSach(ID);
                txtMaSach.Text = sach_dto.Str_MaSach;
                txtTenSach.Text = sach_dto.Str_TenSach;
                txtGiaMua.Text = sach_dto.D_GiaMua.ToString();
                txtGiaBan.Text = sach_dto.D_GiaBan.ToString();

                cbxNXB.SelectedValue = sach_dto.Str_MaNXB;
                cbxTacGia.SelectedValue = sach_dto.Str_MaTacGia;
                cbxTheLoai.SelectedValue = sach_dto.Str_MaTheLoai;

                txtSoLuong.Text = sach_dto.I_SoLuong.ToString();
                txtSoLuongBan.Text = sach_dto.I_SoLuongBan.ToString();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            sach_bus = new Sach_BUS();
            sach_dto = new Sach_DTO();
            sach_dto.Str_MaSach = txtMaSach.Text;
            sach_dto.Str_TenSach = txtTenSach.Text;
            sach_dto.D_GiaMua = decimal.Parse(txtGiaMua.Text);
            sach_dto.D_GiaBan = decimal.Parse(txtGiaBan.Text);
            sach_dto.Str_MaNXB = cbxNXB.SelectedValue.ToString();
            sach_dto.Str_MaTacGia = cbxTacGia.SelectedValue.ToString();
            sach_dto.Str_MaTheLoai = cbxTheLoai.SelectedValue.ToString();
            sach_dto.I_SoLuong = int.Parse(txtSoLuong.Text);
            sach_dto.I_SoLuongBan = int.Parse(txtSoLuongBan.Text);
            sach_dto.I_TrangThai = 0;//luc them moi thi sach o trang thai chua xoa
            //them nha xuat ban
            if (Action.Equals("Insert"))
            {
                if (sach_bus.ThemSach(sach_dto) == 1)
                {
                    MessageBox.Show("Thêm thành công", "Thông báo");
                    if (KhiThemMoi != null)
                    {
                        KhiThemMoi();
                        Form_Them_CapNhat_Sach_Load(sender, e);
                        txtMaSach.Text = sach_bus.MaSachTang();
                    }
                }
                else
                    MessageBoxEx.Show("Thêm thất bại", "Thông báo");
            }
            //cap nhat nha xuat ban
            if (Action.Equals("Update"))
            {
                if (sach_bus.CapNhatSach(sach_dto) == 1)
                {
                    MessageBoxEx.Show("Cập nhật thành công", "Thông báo");
                    if (KhiCapNhat != null)
                    {
                        KhiCapNhat();
                        this.Close();
                    }
                }
                else
                    MessageBoxEx.Show("Cập nhật thất bại", "Thông báo");
            }      
        }
    }
}
